<template>
  <el-main>
    <el-row class="info-line">
      <div class="search-block" style="float: right; display: block">
        <div class="wrapper" style="margin-right: 80%">
          <div class="search-form">
            <div
              class="autocomplete-container"
              style="height: 25px; width: 278px"
            >
              <input
                type="text"
                autocomplete="off"
                name="query"
                placeholder="搜索"
                class="autocomplete-input"
                style="
                  width: 200px;
                  height: 25px;
                  border-radius: 3px 0px 0px 3px;
                "
              />
              <div
                class="autocomplete-button"
                style="height: 25px; line-height: 10px"
              >
                <i class="el-icon-search"></i>
              </div>
            </div>
          </div>
        </div>
      </div>
    </el-row>

    <el-row>
      <el-col :span="6">
        <div class="status-block">
          <div class="title-block">
            <h2>任务状态</h2>
          </div>
          <ul class="nav-block">
            <li
              class="nav-item"
              :class="[activeIndex === 1 ? 'active' : 'unactive']"
            >
              <a @click.prevent="selectListChange(-1)">
                <strong>全部</strong>
              </a>
            </li>
            <li
              class="nav-item"
              :class="[activeIndex === 2 ? 'active' : 'unactive']"
            >
              <a @click.prevent="selectListChange(0)">
                <strong>未开始</strong>
              </a>
            </li>
            <li
              class="nav-item"
              :class="[activeIndex === 3 ? 'active' : 'unactive']"
            >
              <a @click.prevent="selectListChange(1)">
                <strong>运行中</strong>
              </a>
            </li>
            <li
              class="nav-item"
              :class="[activeIndex === 4 ? 'active' : 'unactive']"
            >
              <a @click.prevent="selectListChange(2)">
                <strong>已结束</strong>
              </a>
            </li>
            <li
              class="nav-item"
              :class="[activeIndex === 5 ? 'active' : 'unactive']"
            >
              <a @click.prevent="selectListChange(3)">
                <strong>暂停中</strong>
              </a>
            </li>
          </ul>
        </div>
      </el-col>
      <el-col :span="18">
        <div class="taskList">
          <div
            class="task"
            v-for="(item, index) in items"
            :key="item"
            :offset="index"
          >
            <div class="block-title">
              <h1>{{ item.title }}</h1>
              <span class="pull-right">
                <h3>提交: {{ item.sstatus }}/{{ item.dstatus }}</h3>
                <h3>状态: {{ item.status }}</h3>
                <a href="" @click.prevent="changeDate(index)"
                  >周期: {{ item.stime }} ~ {{ item.etime }}</a
                >
              </span>
            </div>
            <el-button
              class="btn btn-primary"
              @click.native="linkToInfo(item.id)"
              >详情</el-button
            >
            <el-dropdown class="btn-group">
              <el-button
                class="btn btn-default"
                @click.native="linkToResult(item.id)"
              >
                结果<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>已完成情况</el-dropdown-item>
                <el-dropdown-item>未完成情况</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
            <div class="pull-right">
              <a
                href=""
                class="btn btn-success"
                @click.prevent="startTask(item.id, item.status)"
                ><i
                  :class="[
                    item.isStart === '运行'
                      ? 'el-icon-video-play'
                      : 'el-icon-video-pause',
                  ]"
                ></i>
                {{ item.isStart }}</a
              >
              <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
                <span> {{ message }}: </span>
                <el-date-picker
                  type="datetime"
                  v-model="deadline"
                  placeholder="选择一个或多个日期"
                  format="yyyy-MM-dd HH:mm:ss"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  :picker-options="endOptions"
                >
                </el-date-picker>
                <span slot="footer" class="dialog-footer">
                  <el-button @click="dialogVisible = false">取 消</el-button>
                  <el-button type="primary" @click="checkDate()"
                    >确 定</el-button
                  >
                </span>
              </el-dialog>
              <a
                href=""
                class="btn btn-danger"
                @click.prevent="deleteTask(index)"
                ><i class="el-icon-delete"></i> 删除</a
              >
            </div>
          </div>
        </div>

        <div class="block" style="float: right">
          <el-pagination
            @current-change="handleCurrentChange"
            :current-page="pageObject.pageNum"
            :page-size="pageObject.pageSize"
            layout="total, prev, pager, next, jumper"
            :total="pageObject.total"
          >
          </el-pagination>
        </div>
      </el-col>
    </el-row>
  </el-main>
</template>

<script>
import { getTaskList } from "@/api/admin.js";
import { start, pause } from "@/api/admin.js";
import { delTask, countSub, countDSub } from "@/api/brand.js";

const pageParam = {
  list: [],
  pageNum: null,
  pageSize: null,
  total: null,
  totalPage: null,
};
export default {
  data() {
    return {
      items: [],
      multipleSelection: [],
      currentPage: 1,
      pageObject: null,
      activeIndex: 1,
      status: -1,
      dialogVisible: false,
      message: "",
      warn: "",
      deadline: "",
      currentItem: -1,
      endOptions: {
        disabledDate: (time) => {
          
            return time.getTime() <= new Date(this.items[this.currentItem].stime).getTime();
        },
      },
    };
  },

  created() {
    this.pageObject = Object.assign({}, pageParam);
    this.getPageData("", "", -1);
    console.log(this.items);
  },

  methods: {
    logout() {
      this.$store.dispatch("LogOut").then((response) => {});
      this.$router.push({
        path: "/login",
      });
    },
    changeDate(index) {
      this.message = "请选择新的截至日期";
      this.currentItem = index;
      this.dialogVisible = true;
    },
    checkDate(tid) {
      changeDate(tid, this.deadline).then((response) => {
        this.getPageData("", "", -1);
      });
    },
    deleteTask(index) {
      this.$confirm(
        "是否永久删除任务: " + this.items[index].title + "?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        delTask(this.task.id).then((response) => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
      });
    },
    startTask(tid, status) {
      if (status === "已结束") {
        this.message = "该任务已结束，选择新的截至日期以开始任务   ";
        this.dialogVisible = true;
      } else if (status === "运行中") {
        pause(tid).then((response) => {
          this.getPageData("", "", -1);
        });
      } else {
        start(tid, "").then((response) => {
          this.getPageData("", "", -1);
        });
      }
    },
    linkToInfo(id) {
      this.$router.push({
        path: "/adminTaskInfo",
        query: { tid: id },
      });
    },
    linkToResult(id) {
      this.$router.push({
        path: "/adminTaskSlist",
        query: { tid: id },
      });
    },
    selectListChange(status) {
      this.status = status;
      this.activeIndex = status + 2;
      this.getPageData(this.pageObject.pageSize, this.currentPage, status);
    },
    getTaskStatus(status) {
      var name = "";
      switch (status) {
        case 0:
          name = "未开始";
          break;
        case 1:
          name = "运行中";
          break;
        case 2:
          name = "已结束";
          break;
        case 3:
          name = "暂停中";
          break;
      }
      return name;
    },
    getPageData(pageSize, currentPage, status) {
      this.items.splice(0, this.items.length);
      getTaskList(pageSize, currentPage, status).then((response) => {
        let data = response.data;
        this.pageObject.list = data.list;
        this.pageObject.pageNum = data.pageNum;
        this.pageObject.pageSize = data.pageSize;
        this.pageObject.total = data.total;
        this.pageObject.totalPage = data.totalPage;
        for (var i = 0; i < this.pageObject.list.length; i++) {
          let item = {};
          item.id = this.pageObject.list[i].id;
          item.title = this.pageObject.list[i].title;
          item.status = this.getTaskStatus(this.pageObject.list[i].status);
          item.stime = this.pageObject.list[i].createTime;
          item.etime = this.pageObject.list[i].deadline;
          item.sstatus = this.pageObject.list[i].sstatus;
          if (item.sstatus === null) item.sstatus = 0;
          item.dstatus = this.pageObject.list[i].dstatus;
          item.isStart = item.status === "运行中" ? "暂停" : "运行";
          countSub(item.id).then((res1) => {
            item.sstatus = res1.data;
            countDSub(item.id).then((res2) => {
              item.dstatus = res2.data;
              this.items.push(item);
            });
          });
        }
      });
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.getPageData(this.pageObject.pageSize, this.currentPage, this.status);
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
};
</script>

<style scoped>
* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
body {
  margin: 0;
  padding: 0;
  line-height: 24px;
  font: 14px Helvetica Neue, Helvetica, PingFang SC, 微软雅黑, Tahoma, Arial,
    sans-serif;
}

.el-main {
  left: 200px;
  background-color: #eeebf2;
  padding: 20px;
  position: absolute;
  width: 88%;
  bottom: 0;
  top: 60px;
  min-width: 1000px;
}

.autocomplete-input {
  padding: 9px;
  border-radius: 3px;
  font-family: inherit;
  float: left;
  font-size: 1em;
  border: 1px solid rgba(0, 0, 0, 0.19);
  margin: 0;
}

.autocomplete-button {
  font-family: inherit;
  border: none;
  background-color: #990101;
  color: white;
  padding: 8px;
  float: left;
  cursor: pointer;
  border-radius: 0px 3px 3px 0px;
  transition: all 0.2s ease-out 0s;
  margin: 0.5px 0px 0px -1px;
}
.autocomplete-button {
  background-color: #009688 !important;
}

.el-col-6 {
  color: #353a4d;
  padding-left: 15px;
  padding-right: 15px;
}
.status-block {
  padding: 20px;
  padding-bottom: 10px;
  margin: 0 0 10px;
  margin-bottom: 10px;
  background-color: #fff;
  border-top-left-radius: 2px;
  border-top-right-radius: 2px;

  box-shadow: 0 2px 0 rgba(223, 218, 232, 0.5);
}

.el-main .title-block {
  background: rgba(223, 218, 232, 0.15);
  border-bottom-color: #dfdae8;
  margin: -20px -15px 20px;
  margin-left: -20px;
  margin-right: -20px;

  border-bottom: 2px solid #dae0e8;
  border-top-left-radius: 2px;
  border-top-right-radius: 2px;
}

.el-main .title-block h2 {
  display: inline-block;
  font-size: 14px;
  line-height: 1.4;
  margin: 0;
  padding: 10px 15px 9px;
  font-weight: 600;
  text-transform: uppercase;
}

.el-main .title-block .nav-block {
  margin-bottom: 0;
  padding-left: 0;
  list-style: none;
  margin-top: 0;
}

.nav-block {
  margin: 0;
  padding: 0;
}

.nav-block > li {
  float: none;
  display: block;
  position: relative;
}

.nav-block > li > a {
  position: relative;
  display: block;
  padding: 10px 15px;
  margin: 4px 0 0;
}

strong {
  font-weight: 600;
}

.active {
  color: #fff;
  background-color: #009688;
}

.info-line {
  background: rgba(255, 255, 255, 0.75);
  margin: -20px -20px 20px;
  padding: 10px 20px;
}

.el-col-18 {
  padding-left: 15px;
  padding-right: 15px;
  min-height: 1px;
}

.taskList {
  padding: 20px 15px;
  padding-left: 20px;
  padding-right: 20px;
  box-shadow: 0 2px 0 rgba(223, 218, 232, 0.5);
  margin-bottom: 20px;
  margin: 0 0 10px;
  background-color: #ffffff;
  border-top-left-radius: 2px;
  border-top-right-radius: 2px;
}

.task {
  padding: 20px 15px;
  padding-left: 20px;
  padding-right: 20px;
  border: 1px solid #dae0e8;
  -webkit-box-shadow: none;
  box-shadow: none;
  background-color: #ffffff;
  border-top-left-radius: 2px;
  border-top-right-radius: 2px;
  margin: 0 0 10px;
}

.block-title {
  margin: -20px -15px 20px;
  border-bottom: 2px solid #dae0e8;
  border-top-left-radius: 2px;
  border-top-right-radius: 2px;
  background: rgba(223, 218, 232, 0.15);
  margin-left: -20px;
  margin-right: -20px;
}

.block-title h1,
h2,
h3 {
  display: inline-block;
  font-size: 14px;
  line-height: 1.4;
  margin: 0;
  padding: 10px 15px 9px;
  font-weight: 600;
  text-transform: uppercase;
}

.block-title a {
  font-size: 14px;
  line-height: 1.42857143;
  color: #7c62ad;
  background-color: transparent;
}

.pull-right {
  float: right !important;
}

.btn {
  margin: 1px 0;
  background-color: #ffffff;
  border-radius: 3px;
}

.btn-primary {
  background-color: #7c62ad;
  border-color: #7c62ad;
  color: white;
  width: 80px;
}

.btn-success {
  background-color: #afde5c;
  border-color: #afde5c;
  width: 80px;
  color: white;
}

.btn-danger {
  background-color: #de815c;
  border-color: #de815c;
  width: 80px;
  color: white;
}

.btn-default {
  background-color: #eeeeee;
  border-color: #eeeeee;
  width: 80px;
}
.btn {
  display: inline-block;
  margin-bottom: 0;
  font-weight: normal;
  text-align: center;
  vertical-align: middle;
  -ms-touch-action: manipulation;
  touch-action: manipulation;
  cursor: pointer;
  background-image: none;
  border: 1px solid transparent;
  white-space: nowrap;
  padding: 6px 12px;
  font-size: 14px;
  line-height: 1.42857143;
  border-radius: 4px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
</style>